<?php

namespace App\Admin\Controllers;
 

use App\Admin\Models\CustomerModel;

use App\Models\ProjectModel;
use App\Admin\Models\UserModel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

use Illuminate\Http\Request;


class CustomerController extends Controller
{
    use ModelForm;
    public function index(){
        return Admin::content(function (Content $content) {

            $content->header('客户管理');
            $content->description('');
//            $method = isset($_GET['method'])?$_GET['method']:0;
            $content->body($this->grid());
        });
    }
    public function create(){
        return Admin::content(function (Content $content) {

            $content->header('新增客户');
            $content->description('');

            $content->body($this->form());
        });
    }
    
    public function edit(Request $request){
        $this->_id = $request->route('customer');
        return Admin::content(function (Content $content) {

            $content->header('编辑客户');
            $content->description('');
            $content->body($this->form()->edit($this->_id));
        }); 
    }
    
//    public function store(Request $request){
//        $oC = new \App\Admin\Models\CustomerModel;
//        $aC = array();
//        $aC['id'] = $request->get('id');
//        $aC['company'] = $request->get('company');
//        $aC['projectid'] = $request->get('projectid');
//        $aC['startdate'] = $request->get('startdate');
//        $aC['enddate'] = $request->get('enddate');
//        $aC['managerid'] = $request->get('managerid');
//        $aC['address'] = $request->get('address');
//        return $oC->create($aC);
////        $aRes = $oC->update($aC);            //不知道干啥的
//
//    }
    
    public function grid()
    {
//        $this->_method=$method;
        return Admin::grid(CustomerModel::class, function (Grid $grid) {
            
             $grid->resource('admin/customer');

//            $grid->id('ID')->sortable();
            $grid->column('company','公司名称')->sortable();
            $grid->column('projectid','项目名称')->value(function($projectId){
                $oProject = new ProjectModel();
                $oP = $oProject->getProject($projectId);
                return $oP->projectname;
            });
            $grid->column('managerid','项目经理')->value(function($userId){
                $oManager = new ProjectModel(); 
                $oM = $oManager->getManager($userId); 
                return $oM->name;
            });
          
            $grid->column('startdate','开始时间');
             $grid->column('enddate','完成时间');       
             
            $grid->filter(function ($filter) {
    
                  // 设置created_at字段的范围查询
                     $filter->between('startdate', '开始时间')->datetime();
                     $filter->like('company', '公司名称'); 
                     $filter->disableIdFilter();
             });
//             var_dump($this->_method); 
          
             $aMe = Admin::user();
//             var_dump($aMe->id);exit; 
//             $grid->allowEdit();
            $grid->disableRowSelector();
            $grid->disableExport();
            $grid->disableBatchDeletion();
//            $grid->disableActions();
        });
    }
    
    protected function form()
    {
        return Admin::form(CustomerModel::class, function (Form $form) {
            $id = Admin::user()->id;
             $oP = new ProjectModel();
             $aPs = $oP->getManagerNames($id);
             $aJ = $oP->getProjectNames();
//                var_dump($aJ);   
            $form->text('company','公司名称')->rules('required');          
            $form->select('projectid','项目名称')->options($aJ);
            $form->text('address','地址')->rules('required');
            $form->date('startdate','开始时间')->rules('required');
            $form->date('enddate','结束时间');
            $form->select('managerid', '项目经理')->options($aPs);
//            $form->setAction('/admin/projects/save');
//            $form->hidden('assigneeid')->value($id);

//            $form->display('id', 'ID');
 
            

//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
            
            $form->saved(function (Form $form) {
                 return redirect('/admin/customer');
                 });

        });
          
 
    }
}
